package syscore

import (
	"net/http"

	"github.com/EDDYCJY/go-gin-example/pkg/app"
	"github.com/EDDYCJY/go-gin-example/pkg/e"
	"github.com/EDDYCJY/go-gin-example/pkg/util"
	"github.com/EDDYCJY/go-gin-example/service/syscore_service"
	"github.com/gin-gonic/gin"
	"github.com/unknwon/com"
)

type GetInstitutionVerify struct {
}

func GetInstitution(c *gin.Context) {
	var (
		appG = app.Gin{C: c}
		form GetInstitutionVerify
	)
	_, errCode := app.BindAndValidMsg(c, &form)
	if errCode != nil {
		appG.ResponseMsg(errCode, nil)
		return
	}

	Service := syscore_service.GetInstitution{
		PageNum:  util.GetPage(c),
		PageSize: util.GetLimit(c),
	}
	data, err := Service.GetInstitution()
	if err != nil {
		appG.ResponseMsg(err, nil)
		return
	}
	//异步日志
	appG.Response(http.StatusOK, e.SUCCESS, data)

}



type AddInstitutionVerify struct {
	InstitutionName string `alias:"商品名称" form:"institution_name"  json:"institution_name" valid:"Required;"`
	Contact         string `alias:"联系人" form:"contact"  json:"contact" valid:"Required;"`
	UserName        string `alias:"用户名" form:"user_name"  json:"user_name" valid:"Required;"`
	UserPassword    string `alias:"密码" form:"user_password"  json:"user_password" valid:"Required;"`
}

func AddInstitution(c *gin.Context) {
	var (
		appG = app.Gin{C: c}
		form AddInstitutionVerify
	)
	_, errCode := app.BindAndValidMsg(c, &form)
	if errCode != nil {
		appG.ResponseMsg(errCode, nil)
		return
	}

	Service := syscore_service.AddInstitution{
		InstitutionName: form.InstitutionName,
		Contact:         form.Contact,
		UserName:        form.UserName,
		UserPassword:    form.UserPassword,
	}
	data, err := Service.AddInstitution()
	if err != nil {
		appG.ResponseMsg(err, nil)
		return
	}
	//异步日志
	appG.Response(http.StatusOK, e.SUCCESS, data)

}

type EditInstitutionVerify struct {
	ID              int    `form:"id" valid:"Required;"`
	InstitutionName string `alias:"商品名称" form:"institution_name"  json:"institution_name" valid:"Required;"`
	Contact         string `alias:"联系人" form:"contact"  json:"contact" valid:"Required;"`
	UserName        string `alias:"用户名" form:"user_name"  json:"user_name" valid:"Required;"`
	UserPassword    string `alias:"密码" form:"user_password"  json:"user_password" valid:"Required;"`
}

func EditInstitution(c *gin.Context) {
	var (
		appG = app.Gin{C: c}
		form = EditInstitutionVerify{ID: com.StrTo(c.Param("id")).MustInt()}
	)
	_, errCode := app.BindAndValidMsg(c, &form)
	if errCode != nil {
		appG.ResponseMsg(errCode, nil)
		return
	}

	Service := syscore_service.EditInstitution{
		InstitutionID:   form.ID,
		InstitutionName: form.InstitutionName,
		Contact:         form.Contact,
		UserName:        form.UserName,
		UserPassword:    form.UserPassword,
	}
	data, err := Service.EditInstitution()
	if err != nil {
		appG.ResponseMsg(err, nil)
		return
	}
	//异步日志
	appG.Response(http.StatusOK, e.SUCCESS, data)

}

type DeleteInstitutionVerify struct {
	ID int `form:"id" valid:"Required;"`
}

func DeleteInstitution(c *gin.Context) {
	var (
		appG = app.Gin{C: c}
		form = DeleteInstitutionVerify{ID: com.StrTo(c.Param("id")).MustInt()}
	)
	_, errCode := app.BindAndValidMsg(c, &form)
	if errCode != nil {
		appG.ResponseMsg(errCode, nil)
		return
	}

	Service := syscore_service.DeleteInstitution{
		InstitutionID: form.ID,
	}
	data, err := Service.DeleteInstitution()
	if err != nil {
		appG.ResponseMsg(err, nil)
		return
	}
	//异步日志
	appG.Response(http.StatusOK, e.SUCCESS, data)

}
